import com.mit.web.config.Constants;
import com.mit.web.config.DisplayConfiguration;
import com.mit.web.util.FlexUtil;
import com.mit.web.util.MessageUtil;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.PopUpMenuButton;
import mx.controls.ToolTip;
import mx.core.IFlexDisplayObject;
import mx.core.IUIComponent;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
import mx.managers.ToolTipManager;
import mx.resources.ResourceManager;
import mx.rpc.events.FaultEvent;


private var msgTip:ToolTip;

/**
 * 关闭弹出的TitleWindow
 * */
protected function closeWindow(window:IFlexDisplayObject):void{
	PopUpManager.removePopUp(window);
}

/**
 * 处理远程调用的错误
 * */
protected function exceptionHandler(event:FaultEvent):void{
	MessageUtil.popBackEndError(event, this);
}

/**
 * 给任务状态的Popupmenubutton赋值
 * */
protected function taskStateMenuButtonCreationCompleteHandler(event:FlexEvent):void{
	var source:ArrayCollection = new ArrayCollection();
	var all:String = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"label.selectAll");
	source.addItem({label:all});
	source.addItem({type:"separator"});
	source.addAll(Constants.TASK_STATE);
	(event.currentTarget as PopUpMenuButton).dataProvider = source;
}

/**
 * 将isSelected = true的元素从List中删除
 * */
protected function removeSelectedItemFromList(source:ArrayCollection):void{
	//深度克隆一个Array，避免删除过程中的Length变化
	var clone:ArrayCollection = ArrayCollection(FlexUtil.deepClone(source) );
	var countOfDeletedItem:int = 0;
	for(var i:int = 0; i < clone.length; i++){
		var o:Object = clone[i];
		if(o.isSelected){
			//元素删除后，Index发生变化，因此要减去countOfDeletedItem
			source.removeItemAt(i - countOfDeletedItem);
			countOfDeletedItem++;
		}
	}
	clone = null;
}

/**
 * 在指定对象上显示必须输入的错误提示
 * */
protected function showRequiredErrorTip(c:UIComponent):void{
	var requiredMsg:String = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"error.common.requiredField");
	showErrorTip(requiredMsg, c);
}

/**
 * 显示必须输入的错误提示,一般用于多行的组件
 * */
protected function showVerticalRequiredErrorTip(c:UIComponent):void{
	var requiredMsg:String = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"error.common.requiredField");
	showVerticalMiddleErrorTipAt(requiredMsg, c);
}
/**
 * 显示错误提示
 * */
protected function showErrorTip(msg:String, c:UIComponent):void{
	destoryMsgTip();
	
	var p1:Point = new Point(c.x,c.y);
	var p2:Point = c.owner.localToGlobal(p1);
	//Show errorTip
	msgTip = ToolTipManager.createToolTip(
		msg, 
		p2.x + c.width, 
		p2.y, 
		"errorTipRight"
	) as ToolTip;

	
	//Hide errorTip - create方法创建的errortip不会自动消失
	setTimeout(destoryMsgTip,DisplayConfiguration.getInstance().ERROR_TIP_DELAY);
	/*
	如果使用ErrorString,有个问题就是，何时将ErroString要set null，否则会一直显示下去……
	c.errorString = msg;
	c.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
	*/
}

/**
 * 在中间位置显示错误提示，一般用于datagrid对象
 * */
protected function showVerticalMiddleErrorTipAt(msg:String,  c:UIComponent):void{
	destoryMsgTip();
	var p1:Point = new Point(c.x,c.y);
	var p2:Point = c.owner.localToGlobal(p1);
	//Show errorTip
	msgTip = ToolTipManager.createToolTip(
		msg, 
		p2.x + c.width, 
		p2.y + c.height / 2, 
		"errorTipRight"
	) as ToolTip;
	
	
	//Hide errorTip - create方法创建的errortip不会自动消失
	setTimeout(destoryMsgTip,DisplayConfiguration.getInstance().ERROR_TIP_DELAY);

}

/**
 * 显示一般消息提示
 * */
protected function showMessageTip(msg:String, c:UIComponent):void{
	destoryMsgTip();
	
	var p1:Point = new Point(c.x,c.y);
	var p2:Point = c.owner.localToGlobal(p1);
	msgTip = ToolTipManager.createToolTip(msg, p2.x, p2.y - 33, "errorTipTop") as ToolTip;
	msgTip.styleName = "messageTip";
	
	setTimeout(destoryMsgTip,DisplayConfiguration.getInstance().ERROR_TIP_DELAY);
}

/**
 * 在组件右侧显示输入提示！
 * */
protected function showToolTip(msg:String, c:UIComponent, neverHide:Boolean = false):void{
	destoryMsgTip();
	
	var p1:Point = new Point(c.x,c.y);
	var p2:Point = c.owner.localToGlobal(p1);
	msgTip = ToolTipManager.createToolTip(msg, p2.x + c.width + 10, p2.y) as ToolTip;
	msgTip.styleName = "toolTip";
	//很奇怪，正宗ToolTip会出现使用汉字的时候，宽度不够的情况，
	//如果是ErrorTip就不会，因此这里使用了 1.2倍宽度
	msgTip.width = msgTip.width * 1.2;
	//这个问题好像和Flex的Theme有关系。更换Theme为Spark后没有这个现象
	//msgTip.width = msgTip.width;
	if(!neverHide)
		setTimeout(destoryMsgTip,DisplayConfiguration.getInstance().ERROR_TIP_DELAY);
}
/**
 * 隐藏消息提示，一般不能在外部显示调用
 * */
protected function destoryMsgTip():void
{
	if(msgTip){
		
		ToolTipManager.destroyToolTip(msgTip);
		msgTip = null;
		
	}
}

